package com.taxi.driver.module.main.mine.wallet.bill;

import com.qianxx.utils.RxUtil;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.module.vo.BillVO;

import javax.inject.Inject;

public class BillPresenter extends BasePresenter implements BillContract.Presenter {

    private BillContract.View mView;
    private UserRepository mUserRepository;

    private int page = 1;

    @Inject
    public BillPresenter(UserRepository userRepository, BillContract.View view) {
        mUserRepository = userRepository;
        mView = view;
    }

    @Override
    public void refresh(int dateType, String start, String end, String type) {
        mUserRepository.reqBills(page = 1, dateType, start, end, type)
                .map(BillVO::createFrom)
                .compose(RxUtil.applySchedulers())
                .doAfterTerminate(() -> page = 2)
                .subscribe(vo -> mView.setList(vo),
                        ex -> showNetworkError(ex, R.string.network_error, mView, mUserRepository));
    }

    @Override
    public void loadMore(int dateType, String start, String end, String type) {
        mUserRepository.reqBills(page, dateType, start, end, type)
                .map(BillVO::createFrom)
                .compose(RxUtil.applySchedulers())
                .doAfterTerminate(() -> page++)
                .subscribe(vo -> mView.appendList(vo),
                        ex -> showNetworkError(ex, R.string.network_error, mView, mUserRepository));
    }
}
